package cn.zhz.controller;

import cn.zhz.entity.working;
import cn.zhz.service.workingservice;
import com.mysql.jdbc.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping
public class WorkingController {

    private Logger logger = Logger.getLogger(WorkingController.class);

    @Resource
    private workingservice workingservice;

    @RequestMapping(value = "/worklist.html")
    public String getworkList(Model model, @RequestParam(value = "title",required = false) String title){
    int count = 0;
        if(title==null){
            count = 0;
        }
        List<working> worklist = workingservice.getworklist(count);

        if(worklist!=null) {
            model.addAttribute("worklist", worklist);
        }
        return "worklist";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.GET)
    public String add(@ModelAttribute("user")working working){
        return "workadd";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.POST)
    public String addSave(working working,Model model){
        int num = workingservice.Save(working);
        if(num>0){
            model.addAttribute("error","添加成功！");
            return "redirect:/worklist.html";
        }else{
            model.addAttribute("error","添加失败！");
            return "redirect:/add.html";
        }
    }

    @RequestMapping(value = "/selwork.html")
    public String SelworkList(Model model,@RequestParam String id){
        int count = 0;
        if(id!=null) {
            count = Integer.parseInt(id);
        }
        List<working> worklist = workingservice.getworklist(count);
        if(worklist!=null) {
            model.addAttribute("worklist", worklist);
        }
        return "selwork";
    }
}
